<template>
  <div style="display: inline-block;">
    <el-button type="primary" @click="editUser()">修改</el-button>
    <el-dialog :visible.sync="dialogTableVisible" title="修改" width="970px" @close="close">
      <el-form ref="row_data" label-position="left" label-width="90px" style="width: 400px; margin-left:50px;text-align: left">
        <el-form-item v-if="row_data.is_sup_admin !== 1" :label="'用户名'" prop="username">
          <el-input v-model="row_data.username" :disabled="undisabled" placeholder="用户名" />
        </el-form-item>
        <el-form-item v-if="row_data.is_sup_admin !== 1" :label="'管理员权限'" prop="shop_name">
          <el-select v-model="row_data.role_id" class="filter-item" placeholder="暂无权限" style="width: 100%">
            <el-option v-for="(item, index) in shop_role" :key="index" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item :label="'昵称'" prop="nickname">
          <el-input v-model="row_data.nickname" placeholder="昵称" />
        </el-form-item>
        <!-- <el-form-item :label="'手机号码'" prop="phone">
          <el-input v-model="row_data.phone" :disabled="undisabled" placeholder="手机号码" />
        </el-form-item> -->
        <el-form-item label="用户密码" prop="password">
          <el-input
            :type="passwordType"
            v-model="password"
            :placeholder="$t('login.password')"
            name="password"
            auto-complete="on"
            style="width: 200px"
            @keyup.enter.native="handleLogin">
            <span slot="prefix" class="svg-container">
              <svg-icon icon-class="password" />
            </span>
            <span slot="suffix" class="show-pwd" @click="showPwd">
              <svg-icon icon-class="eye" />
            </span>
          </el-input>
        </el-form-item>
        <el-form-item :label="'头像'" prop="head_src">
          <!--<el-alert-->
          <!--title="建议图片长宽比为1:1"-->
          <!--type="warning"-->
          <!--show-icon-->
          <!--close-text="知道了"/>-->
          <el-upload
            :show-file-list="false"
            :on-success="imgSuccess"
            :before-upload="beforeAvatarUpload"
            :http-request="uploadImg"
            :multiple="false"
            action=""
            class="avatar-uploader">
            <img v-if="row_data.head_src" :src="row_data.head_src" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon" />
          </el-upload>
        </el-form-item>
        <!-- <el-form-item :label="'状态'" prop="phone">
          <el-switch :active-value="1" :inactive-value="0" v-model="row_data.status"/>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="submitForm()">编辑</el-button>
          <el-button @click="close()">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import elDragDialog from '@/directive/el-dragDialog' // base on element-ui
import store from '@/store'
import { edit } from '@/api/shop_admin_auth/admin_user'
import { client } from '@/utils/alioss'
// import { getShopRoleList } from '@/api/shop_admin_auth/shopRole'
import { getShopRoleList } from '@/api/shop_admin_auth/shopRole'

export default {
  name: 'DragDialogDemo',
  directives: { elDragDialog },
  props: {
    row_data: {
      type: Object,
      default: function() {
        return null
      }
    }
  },
  data() {
    return {
      dialogTableVisible: false,
      undisabled: true,
      password: null,
      data1: {},
      shop_role: [
        {
          value: 2,
          label: '客服管理员'
        },
        {
          value: 1,
          label: '管理员'
        }
      ],
      passwordType: 'password'
    }
  },
  methods: {
    editUser() {
      this.dialogTableVisible = true
      this.get_shop_role_list()
    },
    get_shop_role_list() {
      getShopRoleList(store.getters.token).then((response) => {
        console.log(response.data.data)
        this.shop_role = response.data.data
      })
      // getShopRoleList(store.getters.token).then(res => {
      //   this.shop_role = res.data.list.data
      // })
    },
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
    },
    uploadImg(file) {
      console.log(file)
      const that = this
      var fileName = 'img' + file.file.uid
      client()
        .put(fileName, file.file)
        .then((result) => {
          that.row_data.head_src = result.url
          that.img = result.url
        })
        .catch((err) => {
          this.$message.error('图片上传失败，原因' + err)
        })
    },
    update_bg(file) {
      var fileName = 'banner_img' + file.file.uid

      client()
        .put(fileName, file.file)
        .then((result) => {
          console.log(result, 'result')
          this.formData.ground_img = result.url
          this.$message.success('主图上传成功！')
        })
        .catch((err) => {
          this.$message.error('图片上传失败，原因' + err)
        })
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isPNG = file.type === 'image/png'
      const isLt2M = file.size / 1024 / 1024 < 2
      if (!isJPG && !isPNG) {
        this.$message.error('上传图片只能是 JPG、PNG 格式!')
        return false
      }
      if (!isLt2M) {
        this.$message.error('上传图片大小不能超过 2MB!')
        return false
      }
      return isJPG | isPNG && isLt2M
    },
    imgSuccess(res, file) {
      this.row_data.head_src = res.url
      // console.log(this.row_data.main_img)
    },
    submitForm() {
      this.data1.id = this.row_data.id
      this.data1.nickname = this.row_data.nickname
      this.data1.password = this.password
      this.data1.head_src = this.row_data.head_src
      this.data1.status = this.row_data.status
      this.data1.role_id = this.row_data.role_id
      console.log(this.data1, 'this.data1')
      console.log(this.row_data.nickName, 'this.row_data.nickName')
      edit(store.getters.token, this.data1).then((res) => {
        if (res.data.status === true) {
          this.$message({
            message: '修改成功',
            type: 'success'
          })
        } else {
          this.$message.error(res.data.msg)
        }
        this.close()
      })
    },
    close() {
      this.$emit('getLists')
      this.dialogTableVisible = false
    }
  }
}
</script>
<style>
.avatar {
  width: 180px;
  height: 180px;
  border-radius: 3%;
  /*margin-top: 10px;*/
}
.avatar-uploader {
  float: left;
  margin-top: 10px;
}
</style>
